/*
 * Тоолбар программы
 */

package mops.toolbar;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.scene.control.ButtonBuilder;
import javafx.scene.control.Separator;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleButtonBuilder;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.ToolBar;
import javafx.scene.control.ToolBarBuilder;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.scene.shape.CircleBuilder;
import mops.commons.Commons;

/**
 *
 * @author Жук Александр
 */
public class GeneralToolBar {
   
    private final ToolBar mainToolBar;
    
    public GeneralToolBar(){
      mainToolBar = createToolBar();
    }
    
  private ToolBar createToolBar() {
    final ToggleGroup alignToggleGroup = new ToggleGroup();
    ToolBar toolBar = ToolBarBuilder.create()
    .items(
        ButtonBuilder.create()
        .id("newButton")
        .graphic(new ImageView(
            new Image(getClass().getResourceAsStream(Commons.APPLICATION_ICON)))
         )
        .tooltip(new Tooltip("New Document... Ctrl+N"))
        .onAction(new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent e) {
                System.out.println("New toolbar button clicked");
            }
        })
        .build(),
        ButtonBuilder.create()
        .id("editButton")
        .graphic(
            CircleBuilder.create()
            .fill(Color.GREEN)
            .radius(8)
            .build()
        )
        .build(),
        ButtonBuilder.create()
        .id("deleteButton")
        .graphic(
            CircleBuilder.create()
            .fill(Color.BLUE)
            .radius(8)
            .build()
        )
        .build(),
        new Separator(Orientation.VERTICAL),
        ToggleButtonBuilder.create()
        .id("boldButton")
        .graphic(
            CircleBuilder.create()
            .fill(Color.MAROON)
            .radius(8)
            .build()
        )
        .onAction(new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent e) {
                ToggleButton tb = ((ToggleButton)e.getTarget());
                System.out.print(e.getEventType() + " occurred on ToggleButton "
                + tb.getId());
                System.out.print(", and selectedProperty is: ");
                System.out.println(tb.selectedProperty().getValue());
            }
        })
        .build(),
        ToggleButtonBuilder.create()
        .id("italicButton")
        .graphic(
        CircleBuilder.create()
            .fill(Color.YELLOW)
            .radius(8)
            .build()
        )
        .onAction(new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent e) {
                ToggleButton tb = ((ToggleButton)e.getTarget());
                System.out.print(e.getEventType() + " occurred on ToggleButton "
                + tb.getId());
                System.out.print(", and selectedProperty is: ");
                System.out.println(tb.selectedProperty().getValue());
            }
        })
        .build(),
        new Separator(Orientation.VERTICAL),
        ToggleButtonBuilder.create()
        .id("leftAlignButton")
        .toggleGroup(alignToggleGroup)
        .graphic(
            CircleBuilder.create()
            .fill(Color.PURPLE)
            .radius(8)
            .build()
        )
        .build(),
        ToggleButtonBuilder.create()
        .id("centerAlignButton")
        .toggleGroup(alignToggleGroup)
        .graphic(
            CircleBuilder.create()
                .fill(Color.ORANGE)
                .radius(8)
                .build()
        )
        .build(),
        ToggleButtonBuilder.create()
        .id("rightAlignButton")
        .toggleGroup(alignToggleGroup)
        .graphic(
            CircleBuilder.create()
                .fill(Color.CYAN)
                .radius(8)
                .build()
        )
        .build()
    )
    .build();
alignToggleGroup.selectToggle(alignToggleGroup.getToggles().get(0));
alignToggleGroup.selectedToggleProperty().addListener(new ChangeListener() {
    @Override
    public void changed(ObservableValue ov, Object oldValue, Object newValue) {
        ToggleButton tb = ((ToggleButton)alignToggleGroup.getSelectedToggle());
            if (tb != null) {
            System.out.println(tb.getId() + " selected");
            }
    }
});

return toolBar;
}
  
   //Получение тоолбара   
    public ToolBar getToolBar() throws Exception {
        if (mainToolBar!=null){
          return mainToolBar;  
        }
        else
        {
          throw new Exception(Commons.GET_TOOLBAR_ERROR);  
        }                
    }
}
